home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 1867 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  1.9 KB

  1. Path: news1.h1.usa.pipeline.com!usenet
  2. From: grantp@usa.pipeline.com(Pete)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Precision question
  5. Date: 13 Jan 1996 15:20:26 GMT
  6. Organization: Pipeline USA
  7. Message-ID: <4d8ijq$e9c@news1.usa.pipeline.com>
  8. NNTP-Posting-Host: pipe10.h1.usa.pipeline.com
  9. X-PipeUser: grantp
  10. X-PipeHub: usa.pipeline.com
  11. X-PipeGCOS: (Pete)
  12. X-Newsreader: Pipeline USA v3.3.0
  13.  
  14. On Jan 13, 1996 14:15:44 in article <Precision question>,
  15. 'dliska@ix.netcom.com (David Liska)' wrote: 
  16.  
  17.  
  18. >I am starting to learn C++ and am a bit confused about how it handles 
  19. >variable precision.  I have the following lines of code: 
  20. >        check1=num1/num2; 
  21. >        check2=int(check1); 
  22. >        if (check1 == check2) 
  23. >The purpose is to see if num1 is divisible by num2 with no remainder. 
  24. >This works fine until I get into the 65k range.  At this point, it 
  25. >calculates every number from there on as "divisible".   
  26.  
  27. From your comments, I gather you're dealing with unsigned integers 
  28. on a 16-bit machine.  Your question really isn't about precicision 
  29. but about the maximum (and minimum if signed) value that can 
  30. be stored in a variable.  On a 16-bit DOS machine, the maximum 
  31. integer size is about 32K.  Declaring the variable unsigned gives 
  32. you one more bit so the max is 64K.  
  33.  
  34. If you need to work with integer values larger than 64K, use long  
  35. or unsigned long variables.  That'll give you 2 and 4 gigs respectively. 
  36.  
  37. >I've tried setting check1 and check2 to long double, but it seems to 
  38. >make no difference. 
  39.  
  40. When you start using floating point variables (float, double,  
  41. long double) you begin to run into problems with the equality 
  42. test ("==").  After doing arithmetic, it is quite likely that two 
  43. variables whose values we humans consider to be equal, 
  44. will not be equal to the machine.  With floats, you often have 
  45. to resort to something like  
  46.  
  47.     if (fabs(v1 - v2) < 0.00000001) 
  48.        cout << "They're EQ!!\n"; 
  49.  
  50. -- 
  51.  
  52. Pete
  53.